package com.shilei.obsever.v3;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:  观察者 在处理事件时可能需要对源对象处理  所以在 event 里面加入源对象
 *                  事件也可以形成继承体系
 * @author: 81271
 * @date: 2020-04-17 20:32
 */
public class Main1 {

    public static void main(String[] args) {
        Children children = new Children();

        children.wakeUp();
    }


}


class Children {
    private boolean cry = false;

    List<Observer> observerList = new ArrayList<>();

    {
        observerList.add(new Dad());
        observerList.add(new Mom());
    }

    boolean isCry() {
        return cry;
    }

    void wakeUp () {
        cry = true;
        WakeUpEvent wakeUpEvent = new WakeUpEvent(System.currentTimeMillis(), "bed", this);
        System.out.println("crying   wuwawuwawuwa!!!!");

        for (Observer observer: observerList) {
            observer.observerWakeUp(wakeUpEvent);
        }
    }
}

interface Observer {
    void observerWakeUp(WakeUpEvent event);
}

abstract class EventObject<T> {
    abstract T getResource();
}

class WakeUpEvent extends EventObject<Children>{
    private long timestamp;
    private String loc;
    private Children source;

    public WakeUpEvent(long timestamp, String loc, Children source) {
        this.timestamp = timestamp;
        this.loc = loc;
        this.source = source;
    }

    @Override
    Children getResource() {
        return source;
    }
}

class Dad implements Observer {
    public void feed() {
        System.out.println("dad  feeding.....");
    }

    @Override
    public void observerWakeUp(WakeUpEvent event) {
        feed();
    }
}

class Mom implements Observer {
    public void tellStory() {
        System.out.println("mom  telling story.....");
    }

    @Override
    public void observerWakeUp(WakeUpEvent event) {
        tellStory();
    }
}
